<script setup>
import { CaretBottom, User, Clock } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import DarkModeSlider from '@/components/DarkModeSlider.vue';
import SmallWindow from '@/components/SmallWindow.vue';
import { userInfoService } from '@/api/user.js';
import useUserInfoStore from '@/stores/userInfo.js';
import { useTokenStore } from '@/stores/token.js';
import { useThemeStore } from '@/stores/theme.js';
import { computed } from 'vue';

const router = useRouter();
const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();
const themeStore = useThemeStore();
const logoUrl = computed(() => themeStore.logoUrl);

// 获取用户信息
const getUserInfo = async () => {
  const userId = userInfoStore.info.userId;
  if (!userId) {
    console.error('用户 ID 未定义');
    return;
  }
  try {
    const result = await userInfoService(userId);
    userInfoStore.setInfo(result.data.data);
  } catch (error) {
    console.error('获取用户信息失败:', error);
  }
};

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('您确认要退出吗?', '温馨提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(() => {
        tokenStore.removeToken();
        userInfoStore.removeInfo();
        localStorage.removeItem('role');
        router.push('/login');
        ElMessage.success('退出登录成功');
      })
      .catch(() => {
        ElMessage.info('用户取消了退出登录');
      });
  } else {
    router.push(command);
  }
};

getUserInfo();
</script>

<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <div class="aside-first">
        <el-image :src="logoUrl" :fit="'scale-down'" />
      </div>

      <el-menu
          active-text-color="#ffd04b"
          background-color="#232323"
          text-color="#fff"
          :default-active="$route.path"
          router>
        <!-- 用户管理菜单（保留） -->
        <el-sub-menu index="admin-users">
          <template #title>
            <el-icon>
              <User />
            </el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/admin/users">
            <el-icon>
              <User />
            </el-icon>
            <span>用户列表</span>
          </el-menu-item>
        </el-sub-menu>

        <!-- 新增：时间管理菜单 -->
        <el-sub-menu index="admin-time">
          <template #title>
            <el-icon>
              <Clock />
            </el-icon>
            <span>时间管理</span>
          </template>
          <el-menu-item index="/admin/time">
            <el-icon>
              <Clock />
            </el-icon>
            <span>学习记录</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <div><strong>{{ userInfoStore.info.username }}</strong></div>
        <dark-mode-slider></dark-mode-slider>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar
                :src="userInfoStore.info.avatarUrl ? userInfoStore.info.avatarUrl : 'https://img2.baidu.com/it/u=3926734404,2114692953&fm=253&fmt=auto&app=138&f=JPEG?w=807&h=800'"/>
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <el-main class="el-main">
        <!-- 通过路由渲染对应页面内容 -->
        <router-view></router-view>


<!-- 四图展示区域 -->
        <div class="image-container">
          <el-image 
           src="/src/assets/images/image1.png" 
            fit="cover"
            class="image-item"
          />
          <el-image 
           src="/src/assets/images/image2.png"
            fit="cover"
            class="image-item"
          />
          <el-image 
           src="/src/assets/images/image3.png"
            fit="cover"
            class="image-item"
          />
          <el-image 
            src="/src/assets/images/image4.png"
            fit="cover"
            class="image-item"
          />
        </div>




      </el-main>
      <el-footer>基于Deepseek的408考研小助手</el-footer>
    </el-container>
    <small-window></small-window>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    .aside-first {
      @include hold(100, 10);
      @include position-center-box("row,column");
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: $global-background-color;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-main {
    background-color: $global-background-color;
    padding: 20px;
  }
.image-container {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 20px;
      margin-top: 30px;
    }
    
    .image-item {
      width: 100%;
      height: 200px;
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }
  
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>